package com.example.travelservlet.commentDynamic.service;

import com.example.travelservlet.commentDynamic.dao.CommentDynamicMapper;
import com.example.travelservlet.commentEntertainment.dao.CommentMapper;
import com.example.travelservlet.entertainment.repository.EntertainmentRepository;
import com.example.travelservlet.entity.*;
import com.example.travelservlet.user.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author hxy
 * @date 2023/5/16 9:04
 */
@Service
public class CommentDynamicService {
    @Autowired
    private CommentDynamicMapper commentDynamicMapper;
    @Autowired
    private UserRepository userRepository;




    public Integer insertDynamicComment(DynamicComment dynamicComment){
        return commentDynamicMapper.insertDynamicComment(dynamicComment);
    }

    public List<DynamicCommentDto> findDynamicCommentByDynamicId(String dynamicId){
        List<DynamicComment> dynamicCommentList = commentDynamicMapper.findDynamicCommentByDynamicId(dynamicId);

        List<DynamicCommentDto> dynamicCommentDtoList = new ArrayList<>();
        for (DynamicComment dynamicComment : dynamicCommentList){
            DynamicCommentDto dynamicCommentDto = new DynamicCommentDto();
            Optional<User> optionalUser = userRepository.findById(dynamicComment.getUserId());
            Dynamic dynamic = commentDynamicMapper.findDynamicByDynamicId(dynamicComment.getDynamicId());
            User user = optionalUser.get();
            dynamicCommentDto.setUser(user);
            dynamicCommentDto.setDynamic(dynamic);
            BeanUtils.copyProperties(dynamicComment,dynamicCommentDto);
            dynamicCommentDtoList.add(dynamicCommentDto);
        }
        return dynamicCommentDtoList;
    }

    public List<DynamicComment> findAllDynamicComments() {
        return commentDynamicMapper.findAllDynamicComments();
    }
    public List<DynamicComment> findAllDynamicComments1(int page,int size) {
        int pageIndex = (page-1)*size;
        return commentDynamicMapper.findAllDynamicComments1(pageIndex,size);
    }

    public List<DynamicComment> findCommentsByKeyWord(String key) {
        return commentDynamicMapper.findCommentsByKeyWord(key);
    }

    public void deleteDynamicCommentById(String commentId) {
        commentDynamicMapper.deleteDynamicCommentById(commentId);
    }

    public List<DynamicComment> findCommentsByKeyWord1(@Param("key") String key, @Param("page") Integer page,@Param("size") Integer size) {
        Integer pageIndex = (page-1)*size;
        return commentDynamicMapper.findCommentsByKeyWord1(key,pageIndex,size);
    }
}
